//
//  SendMessageView.m
//  i5db
//
//  Created by iMuse on 15-5-27.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "SendMessageView.h"
#import "AppCenter.h"
@implementation SendMessageView
{
    UIImageView * imageView;
    UITextView  * textView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initView:frame];
    }
    return self;
}

+ (void)popupAnimation:(UIView *)outView duration:(CFTimeInterval)duration
{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];
    
    NSMutableArray * values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    
    [outView.layer addAnimation:animation forKey:nil];
}


-(void)tapSendMessageView:(UITapGestureRecognizer*) gester
{
    
    [self removeSendMessageView];
}

-(void) removeSendMessageView
{
        [self removeFromSuperview];
}

-(void)sendMessageButton:(UIButton*) button
{
    
}

-(void) initView:(CGRect) frame
{
    UITapGestureRecognizer * gester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSendMessageView:)];
    [self addGestureRecognizer:gester];
    
    imageView = [[UIImageView alloc] init];
    imageView.frame = frame;
    imageView.backgroundColor = [AppCenter color:@"235,235,241,1"];
    imageView.alpha = 0.9;
    [self addSubview:imageView];
    
    
    textView = [UITextView newAutoLayoutView];
    textView.layer.borderColor = [AppCenter color:@"188,188,188"].CGColor;
    textView.layer.borderWidth = 0.5;
    textView.layer.cornerRadius = 4;
    [self addSubview:textView];
    
    [textView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [textView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:80];
    [textView autoSetDimensionsToSize:CGSizeMake(SCREEN_WIDTH-80, SCREEN_HEIGHT/4)];
    
    UIButton * sendButton = [UIButton newAutoLayoutView];
    [sendButton addTarget:self action:@selector(sendMessageButton:) forControlEvents:UIControlEventTouchDragInside];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    sendButton.layer.cornerRadius = 4;
    sendButton.layer.borderWidth = 1;
    sendButton.titleLabel.font = [UIFont systemFontOfSize:12];
    sendButton.layer.borderColor = [AppCenter color:@"76,187,248,1"].CGColor;
    [sendButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [self addSubview:sendButton];
    [sendButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textView withOffset:10];
    [sendButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:40];
    [sendButton autoSetDimensionsToSize:CGSizeMake(SCREEN_WIDTH-80, 40)];
   
    
}


+(UIViewController*)rootController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

+(void)sendMessage
{
    
    UIViewController * rootController = [self rootController];
    
    SendMessageView * sendMessage = [[SendMessageView alloc] initWithFrame:rootController.view.bounds];
    
    [rootController.view addSubview:sendMessage];
    [self popupAnimation:sendMessage duration:0.5];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

@end
